﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Entities;
using C.Unite;
using ChanExt;
using MySoft.Data;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.SystemMgr
{
    public partial class DataBaseGrid : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                JObject defaultObj = new JObject();
                defaultObj.Add("BaseValue", "");
                defaultObj.Add("BaseName", "");
                defaultObj.Add("OrderID", 0);
                defaultObj.Add("ReMark", "");
                btnNew.OnClientClick = Grid1.GetAddNewRecordReference(defaultObj, false);
                BindGrid(GetWhere());
            }
        }

        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = TbsDbSession.Default.From<Sys_BaseData>().Where(wc).Count();

            List<Sys_BaseData> items = TbsDbSession.Default.From<Sys_BaseData>().Where(wc).OrderBy(Sys_BaseData._.OrderID.Asc, Sys_BaseData._.ID.Desc).ToList();

            //// 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }

        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }


        protected void Grid1_RowDataBound(object sender, ChanExt.GridRowEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
        }

        protected void Grid1_RowCommand(object sender, ChanExt.GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            Sys_BaseData sp = TbsDbSession.Default.From<Sys_BaseData>().Where(Sys_BaseData._.ID == id).ToFirst();
            if (e.CommandName == "Del")
            {
                TbsDbSession.Default.Delete<Sys_BaseData>(Sys_BaseData._.ID == id);
                //Alert.ShowInTop("删除成功！");
                BindGrid(GetWhere());
            }
        }

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            if (!string.IsNullOrEmpty(Request["gid"]))
            {
                wc = wc && Sys_BaseData._.GroupID == int.Parse(Request["gid"]);
            }
            else
                wc = wc && Sys_BaseData._.GroupID == 0;
            return wc;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["gid"]))
            {
                int tid = int.Parse(Request["gid"]);
                string tno = Request["gno"];
                Dictionary<int, Dictionary<string, string>> modifiedDict = Grid1.GetModifiedDict();
                foreach (int rowIndex in modifiedDict.Keys)
                {
                    int rowID = Convert.ToInt32(Grid1.DataKeys[rowIndex][0]);
                    UpdateDataRow(modifiedDict[rowIndex], rowID, tid, tno);
                }

                // 新增数据
                List<Dictionary<string, string>> newAddedList = Grid1.GetNewAddedList();
                for (int i = newAddedList.Count - 1; i >= 0; i--)
                {
                    UpdateDataRow(newAddedList[i], 0, tid, tno);
                }
                BindGrid(GetWhere());
                Alert.Show("保存成功！");
            }
            else
            {
                Alert.Show("请选择类别进行添加！");
            }
        }

        private static void UpdateDataRow(Dictionary<string, string> rowDict, int Dtype, int tid, string no)
        {
            Sys_BaseData sbd = new Sys_BaseData();
            if (Dtype == 0)
            {
                sbd.DeAttach();
            }
            else
            {
                sbd.ID = Dtype;
                sbd.Attach();
            }
            if (rowDict.ContainsKey("BaseValue"))
            {
                sbd.BaseValue = rowDict["BaseValue"];
            }
            if (rowDict.ContainsKey("BaseName"))
            {
                sbd.BaseName = rowDict["BaseName"];
            }
            if (rowDict.ContainsKey("OrderID"))
            {
                sbd.OrderID = int.Parse(rowDict["OrderID"]);
            }
            if (rowDict.ContainsKey("ReMark"))
            {
                sbd.ReMark = rowDict["ReMark"];
            }
            if (tid > 0)
            {
                sbd.GroupID = tid;
                sbd.GroupNo = no;
            }
            if (Dtype == 0)
            {
                TbsDbSession.Default.Insert<Sys_BaseData>(sbd);
            }
            else
            {
                TbsDbSession.Default.Update<Sys_BaseData>(sbd);
            }
        }

    }
}